package com.szh.mysobproject.ui.activity.msg

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.dylanc.longan.lifecycleOwner
import com.szh.mysobproject.R
import com.szh.mysobproject.action.OnBack2TopListener
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.databinding.AtMeMsgListActivityBinding
import com.szh.mysobproject.ui.activity.BrowserActivity
import com.szh.mysobproject.ui.activity.FishPondDetailActivity
import com.szh.mysobproject.ui.adapter.AdapterDelegate
import com.szh.mysobproject.ui.adapter.AtMeMsgAdapter
import com.szh.mysobproject.util.*
import com.szh.mysobproject.viewmodel.MsgViewModel
import com.szh.mysobproject.widget.StatusLayout
import kotlinx.coroutines.flow.collectLatest

/**
 * @author : create by  szh
 * @date : 2022/7/12 14:43
 * @desc :@我 消息列表界面
 */

class AtMeMsgListActivity : AppActivity(), StatusAction, OnBack2TopListener {

    private val mBinding by viewBinding<AtMeMsgListActivityBinding>()
    private val mMsgViewModel by viewModels<MsgViewModel>()
    private val mAdapterDelegate = AdapterDelegate()
    private val mAtMeMsgAdapter = AtMeMsgAdapter(mAdapterDelegate)
    private val loadStateListener = loadStateListener(mAtMeMsgAdapter) {
        mBinding.refreshLayout.finishRefresh()
    }

    override fun getLayoutId(): Int = R.layout.at_me_msg_list_activity

    override fun initView() {
        mBinding.rvAtMeMsgList.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAtMeMsgAdapter
            addItemDecoration(SimpleLinearSpaceItemDecoration(1.dp))
        }
    }

    override fun initData() {
        lifecycleOwner.lifecycleScope.launchWhenCreated {
            mMsgViewModel.getAtMeMsgList().collectLatest {
                mAtMeMsgAdapter.submitData(it)
            }
        }
    }

    override fun initEvent() {
        titleBar?.setDoubleClickListener {
            onBack2Top()
        }
        mBinding.refreshLayout.setOnRefreshListener {
            mAtMeMsgAdapter.refresh()
        }
        mAtMeMsgAdapter.addLoadStateListener(loadStateListener)
        mAdapterDelegate.setOnItemClickListener { _, position ->
            val item = mAtMeMsgAdapter.snapshot()[position] ?: return@setOnItemClickListener
            mMsgViewModel.readAtMeMsg(item.id).observe(this) {}
            when (item.type) {
                "moment" -> FishPondDetailActivity.start(this, item.exId)
                "wenda" -> {
                    val url = "$SUNNY_BEACH_QA_URL_PRE${item.exId}"
                    BrowserActivity.start(this, url)
                }
                else -> {}
            }
        }
    }

    override fun getStatusLayout(): StatusLayout = mBinding.hlAtMeMsgHint

    override fun onDestroy() {
        super.onDestroy()
        mAtMeMsgAdapter.removeLoadStateListener(loadStateListener)
    }

    override fun onBack2Top() {
        mBinding.rvAtMeMsgList.smoothScrollToPosition(0)
    }
}